package fileReader.demo.reader;

import java.io.DataInputStream;
import java.io.IOException;

public class PolyLineReader extends LayerReader{

	/**
	 * Method reads pipeline specific data from main file (.shp)
	 * For more information read ESRI Technical Dscription 
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param fileLength length of file to read
	 * @author Tero Maijala
	 */
	public void readPolyLineData(DataInputStream dataIn, String layer, MapLayer mapLayer, int fileLength) {
		try {
			int i = 100;									// mainheader is already read
			while(i<fileLength)
			{
				i = i + 8;
				
				dataIn.readInt();						//rec header
				int contLength = 2*(dataIn.readInt());	//rec header
				readLittleInteger(dataIn.readInt());	//shapetype	
				
				Box bb = makeBoundingBox(dataIn);
				
				int numOfParts = readLittleInteger(dataIn.readInt());	//number of parts
				int numOfPoints = readLittleInteger(dataIn.readInt());	//number of points
				
				dataIn.skipBytes(numOfParts*4);							//skip some pointers
											
				if(layer.contentEquals("pipeline")){
					createPipeline(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("coastline")){
					createCoastLine(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("bridge")){
					createBridge(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("depthcontour")){
					createDepthContour(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("navigline")){
					createNavigLine(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("limitl")){
					createLimitL(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("river")){
					createRiver(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("roadnrailw")){
					createRoadNRailw(numOfPoints, bb, dataIn, mapLayer);
				}
				else if(layer.contentEquals("leadingline")){
					createLeadingLne(numOfPoints, bb, dataIn, mapLayer);
				}
				
				
				i = i + contLength;
			}
		} 
		catch (IOException e) 
		{
			readDataError();
			System.out.println("polylinereader" + layer);
		}
		
	}


	
	
	private void createBridge(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer){
		PolyLineDrawer bridge = new PolyLineDrawer();	
		bridge.setBoundingBox(bb);

		for(int k=0; k<numOfPoints; k++)
		{
			bridge.getVertexList().add(makeVertex(dataIn));
			
		}
		mapLayer.data.add(bridge);
	}
	
	private void createCoastLine(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer){
		PolyLineDrawer coastline = new PolyLineDrawer();
		coastline.setBoundingBox(bb);

		for(int k=0; k<numOfPoints; k++)
		{
			coastline.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(coastline);
		
	}
	
	private void createPipeline(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer pipeline = new PolyLineDrawer();
		pipeline.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			pipeline.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(pipeline);
	}
	
	private void createDepthContour(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer polyline = new PolyLineDrawer();
		polyline.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			polyline.getVertexList().add(makeVertex(dataIn));
		}
		
		mapLayer.data.add(polyline);
	}

	private void createNavigLine(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer navigline = new PolyLineDrawer();
		navigline.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			navigline.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(navigline);
	}
	
	private void createLimitL(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer limit = new PolyLineDrawer();
		limit.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			limit.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(limit);
	}
	
	private void createRiver(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer river = new PolyLineDrawer();
		river.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			river.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(river);
	}
	
	private void createRoadNRailw(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer roadnrailw = new PolyLineDrawer();
		roadnrailw.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			roadnrailw.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(roadnrailw);
	}
	
	private void createLeadingLne(int numOfPoints, Box bb, DataInputStream dataIn, MapLayer mapLayer) {
		PolyLineDrawer leadinlne = new PolyLineDrawer();
		leadinlne.setBoundingBox(bb);
	
		for(int k=0; k<numOfPoints; k++)
		{
			leadinlne.getVertexList().add(makeVertex(dataIn));
		}
		mapLayer.data.add(leadinlne);
	}
	
	/**
	 * Method reads bridge specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readBridgeAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
		for(int n=0; n<numOfRecords; n++)
		{
			char value = (char)dataIn.readByte(); //record deleted flag
			if(value == 42)
			{
				recordDeleted();
			}	
			skipLayerBytes(dataIn);
			
			dataIn.skipBytes(32);	// INT_NAME
			
			dataIn.skipBytes(11);	// STATUSCNST
			
			dataIn.skipBytes(5);	// HORCLR
			
			dataIn.skipBytes(32);	// NAMEBRIDGE
			
			dataIn.skipBytes(11);	// TYPEBRIDGE
			
			dataIn.skipBytes(32);	// FI_OWNER
			
			dataIn.skipBytes(5);	// VERCCL
			
			dataIn.skipBytes(11);	// TYPEROAD
			
		}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}

	
	/**
	 * Method reads coastline specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readCoastLineAttributes(DataInputStream dataIn, int numOfRecords) {
		try{
		for(int n=0; n<numOfRecords; n++)
		{
			char value = (char)dataIn.readByte(); //record deleted flag
			if(value == 42)
			{
				recordDeleted();
			}	
			skipLayerBytes(dataIn);
			
			dataIn.skipBytes(7);	// ELEVCOALNE
			
			dataIn.skipBytes(11);	// TYPECOALNE
			
			dataIn.skipBytes(32);	// VERDAT
	
		}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader/coastlineattributes");
		}
	}
	
	
	/**
	 * Method reads pipeline specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Tero Maijala
	 */
	public void readPipeLineAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
		for(int n=0; n<numOfRecords; n++)
		{
			char value = (char)dataIn.readByte(); //record deleted flag
			if(value == 42)
			{
				recordDeleted();
			}	
			
			skipLayerBytes(dataIn);
			
			dataIn.skipBytes(11);	// TYPEPIPSOL
			
			dataIn.skipBytes(32);	// FI_OWNER
			
			dataIn.skipBytes(4);	// BURDEPPIPE
			
		}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}
	
	/**
	 * Method reads depth contour specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readDepthContourAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(11);	// QUADEPCNT
				
				dataIn.skipBytes(11);	// CONTCRIT
				
				dataIn.skipBytes(11);	// FI_TYSRCDA
				
				dataIn.skipBytes(11);	// METHCNTRNG
				
				dataIn.skipBytes(12);	// TRUEDEPTH
				
				String depthString = "";	//DEPTH
				for(int m=0; m<6; m++)
				{
					depthString = depthString + (char)dataIn.readByte();	
				}
				
				double depth = Double.parseDouble(depthString);
				((PolyLineDrawer) layer.data.get(n)).setDepth(depth);
			}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}

	/**
	 * Method reads navigline specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Tero Maijala
	 */
	public void readNavigLineAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(5);	// NAVLINEDEP
				
				dataIn.skipBytes(11);	// PRIMFAIRWY
				
				dataIn.skipBytes(11);	// FI_RADIUS
				
				dataIn.skipBytes(6);	// BEARING
				
				dataIn.skipBytes(11);	// CATEGNAV
				
				dataIn.skipBytes(11);	// TYPENAVLNE
				
			}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}

	/**
	 * Method reads limitline specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Tero Maijala
	 */
	public void readLimitLAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(32);	// INT_NAME
				
				dataIn.skipBytes(128);	// NAMELIMIT
				
				dataIn.skipBytes(11);	// TYPELIMIT
				
			}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}


	

	/**
	 * Method reads river specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readRiverAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(32);	// INT_NAME
				
				String nameRiver = "";	//NAMERIVER
				for(int m=0; m<32; m++)
				{
					nameRiver = nameRiver + (char)dataIn.readByte();
				}
				((PolyLineDrawer) layer.data.get(n)).setRiverName(nameRiver);
				
				dataIn.skipBytes(11);	// DIRECTFLOW
				
				dataIn.skipBytes(11);	// QUASURVEY
			}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}

	/**
	 * Method reads roadnrailw specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readRoadNRailwAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(11);	// STATUSCNST
				
				String idRoad = "";		// IDROAD
				for(int m=0; m<32; m++)
				{
					idRoad = idRoad + (char)dataIn.readByte();
				}
				((PolyLineDrawer) layer.data.get(n)).setRoadName(idRoad);
				
				dataIn.skipBytes(11);	// TYPEROAD
				
				
			}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}

	/**
	 * Method reads leadinglne specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readLeadingLneAttributes(DataInputStream dataIn, PolyLineMapLayer layer, int numOfRecords) {
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(17);	// NAVLNEOID
				
				dataIn.skipBytes(17);	// MDISTOID
				
				dataIn.skipBytes(11);	// TYPLEADLNE
				
				dataIn.skipBytes(6);	// BEARING
				
				dataIn.skipBytes(11);	// CATEGNAV
				
			}
		}catch (IOException e) {
			readDataError();
			System.out.println("polylinereader" + layer);
		}
	}

}
